package com.imchen.cms.cmsweb.controller;

import com.imchen.cms.annotation.LogTrack;
import com.imchen.cms.annotation.UserLoginToken;
import com.imchen.cms.exception.CmsException;
import com.imchen.cms.model.User;
import com.imchen.cms.service.UserService;
import com.imchen.cms.utils.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * @author imchen
 * @create: 2019-01-08 19:25
 **/
@RestController
@RequestMapping(value = "/user")
public class UserController implements BaseController<User> {

    private final UserService userService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Object addUser(@RequestBody User user) throws CmsException {
        return userService.add(user);
    }

    @PostMapping(value = "/login")
    @LogTrack(value = "用户登录")
    public HashMap<String,String> login(@RequestBody User user) throws Exception {
         return userService.login(user);
    }

    @Override
    @UserLoginToken
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HashMap add(@RequestBody User user) throws CmsException {
        return userService.add(user);
    }

    @Override
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.DELETE)
    public int remove(@PathVariable int id) {
        return userService.removeById(id);
    }

    @Override
    @UserLoginToken
    @RequestMapping(value = "/delete/{id}")
    public int delete(@PathVariable int id) {
        return userService.deleteByPrimaryKeyLogic(id);
    }

    @Override
    @UserLoginToken
    @RequestMapping(value = "/update")
    public int update(@RequestBody User user) {
        return userService.update(user);
    }

    @Override
    @UserLoginToken
    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    public HashMap<String, Object> find(@PathVariable int id) {
        return ServiceUtil.toObjectMap(userService.findById(id),"user");
    }

    @UserLoginToken
    @GetMapping(value = "/find/users")
    public HashMap<String, Object> findUsers(@RequestParam int index,@RequestParam int pageSize) {
        return ServiceUtil.toObjectMap(userService.findUsers(index,pageSize),"user");
    }

    @GetMapping("/find/pages")
    @UserLoginToken
    public HashMap<String, Object> findByPage(@RequestParam int index, @RequestParam int pageSize){
        return ServiceUtil.toObjectMap(userService.findByPageHelper(index,pageSize),"user");
    }

    @UserLoginToken
    @GetMapping("/find/username/{username}")
    public Object findByName(@PathVariable("username") String username) {
        logger.debug("request username:{}", username);
        return userService.findByName(username);
    }

    @UserLoginToken
    @RequestMapping(value = "/remove/username/{username}", method = RequestMethod.DELETE)
    public Object removeUser(@PathVariable String username) {
        return userService.deleteByName(username);
    }

    @UserLoginToken
    @RequestMapping(value = "/delete/username/{username}", method = RequestMethod.DELETE)
    public Object deleteUser(@PathVariable String username) {
        return userService.deleteByUserNameLogic(username);
    }

    @UserLoginToken
    @DeleteMapping("/restore/{id}")
    public int restore(@PathVariable int id) {
        return userService.restoreById(id);
    }
}